package com.infocollect.project.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappActionRoleEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.mapper.TappActionRoleMapper;
import com.infocollect.project.api.req.DelActionRoleReq;
import com.infocollect.project.api.req.TappActionRoleReq;
import com.infocollect.project.api.service.ITappActionRoleService;
import com.infocollect.project.api.service.ITappMenuActionService;
import com.infocollect.project.api.vo.ActionRoleVO;
import com.infocollect.project.api.vo.UserButtonActionVO;
import com.infocollect.project.form.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 角色菜单按钮权限
 */
@Service
public class TappActionRoleServiceImpl extends ServiceImpl<TappActionRoleMapper, TappActionRoleEntity> implements ITappActionRoleService {
    @Autowired
    private TokenService tokenService;
    /**
     * 角色菜单按钮
     *
     * @param req
     * @return
     */
    @Override
    public R<String> saveActionRole(TappActionRoleReq req) {
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        for (int i = 0; i < req.getActionList().size(); i++) {
            Long actionId = req.getActionList().get(i);
            TappActionRoleEntity entity = new TappActionRoleEntity();
            entity.setFcreateby(currentUserInfo.getUserName());
            entity.setFlastupdateby(currentUserInfo.getUserName());
            entity.setFcreationdate(new Date());
            entity.setFlastupdatedate(new Date());
            entity.setActionId(actionId);
            entity.setRoleId(req.getRoleId());
            baseMapper.insert(entity);
        }
        return R.newOk();
    }
    /**
     * 获取用户拥有的按钮权限
     *
     * @param
     * @return
     */
    @Override
    public List<UserButtonActionVO> getCurrentMenuAction() {
        return baseMapper.getCurrentMenuAction(CurrentHelp.Ins.GetUser().getDeptcode(), CurrentHelp.Ins.GetUser().getOrgancode(), CurrentHelp.Ins.GetUser().getUserName());
    }

    ///根据角色和菜单ID 获取角色拥有哪些按钮权限
    @Override
    public List<ActionRoleVO> queryByRoleMenuId(Long roleId, Long menuId) {
        return baseMapper.queryByRoleMenuId(roleId, menuId);
    }

    @Autowired
    private ITappMenuActionService menuActionService;

    /**
     * 删除菜单按钮权限
     *
     * @param
     * @return
     */
    @Override
    public R<String> delActionRole(DelActionRoleReq param) {
       baseMapper.deleteById(param.getId());
        return R.newOk();
    }


}
